package optimizations.parser.SyntaxTree;

import java.io.IOException;

import execution.Tape;
import optimizations.lexer.Position;

public class TapeOperationNode extends AbstractSyntaxNode {
	
	public TapeOperationType getType() {
		return type;
	}

	@Override
	public String toString() {
		return "TapeOperationNode [type=" + type + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TapeOperationNode other = (TapeOperationNode) obj;
		if (type != other.type)
			return false;
		return true;
	}

	private final TapeOperationType type;

	public TapeOperationNode(Position position, TapeOperationType type) {
		super(position);
		this.type = type;
	}

	@Override
	public void execute(Tape tape) throws IOException {
		switch(this.type) {
		case INC_PTR: tape.inc_ptr(); break;
		case DEC_PTR: tape.dec_ptr(); break;
		case INC: tape.inc(); break;
		case DEC: tape.dec(); break;
		case OUT: tape.out(); break;
		case IN: tape.in(); break;
		}
	}

}
